package com.fast.mybatis;

import com.fast.mybatis.service.FastService;
import com.fast.mybatis.util.DateUtils;
import com.fast.mybatis.util.MySqlDBUtil;
import com.fast.mybatis.util.MysqlEntity;
import com.fast.mybatis.util.TableEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

/**
 * @Author likaixuan
 * @Description
 * @Create Date  2018/9/4 22:07
 * @Version 1.0
 */
@Controller
@CrossOrigin
public class IndexController {

    @Autowired
    private FastService fastService;

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("/demo")
    @ResponseBody
    public Map<String, Object> demo() {
        Map<String, Object> map = new HashMap<>();
        map.put("UserName", "888888888.00");
        map.put("PassWord", "123456");
        System.out.println(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") + " - " + map);
        return map;
    }


    @RequestMapping("testConn")
    @ResponseBody
    public Map testConn(MysqlEntity mysqlEntity) {
        MySqlDBUtil mySqlDBUtil = new MySqlDBUtil(mysqlEntity);
        Connection connection = mySqlDBUtil.getConnection();

        Map map = new HashMap();
        if (null != connection) {
            map.put("code", 200);
            map.put("msg", "连接成功");
            map.put("list", mySqlDBUtil.getDbNames(connection));
        } else {
            map.put("code", "500");
            map.put("msg", "连接失败，代码错误");
        }
        return map;

    }

    /**
     * 释放数据库链接
     */
    @RequestMapping("closeDB")
    @ResponseBody
    public Map closeDB() {
        MySqlDBUtil.closeConnection();
        Map map = new HashMap();
        map.put("code", 200);
        map.put("msg", "重置成功");

        return map;

    }

    @RequestMapping("getTableNames")
    @ResponseBody
    public Map getTableNames(MysqlEntity mysqlEntity) {
        MySqlDBUtil mySqlDBUtil = new MySqlDBUtil(mysqlEntity);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "连接成功");
        List<TableEntity> templist = mySqlDBUtil.getTableNames(mysqlEntity.getDbName());
        List<TableEntity> list = new ArrayList<>();
        for (TableEntity tableEntity : templist) {
            if (tableEntity.getTableName().contains(mysqlEntity.getKeyWord())) {
                list.add(tableEntity);
            }
        }
        map.put("data", list);
        map.put("count", 10);
        return map;
    }

    @RequestMapping("getColumnNames")
    @ResponseBody
    public Map getColumnNames(MysqlEntity mysqlEntity) {
        MySqlDBUtil mySqlDBUtil = new MySqlDBUtil(mysqlEntity);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "连接成功");
        map.put("data", mySqlDBUtil.getColumnNames(mysqlEntity.getDbName(), mysqlEntity.getTableName(), "_"));
        map.put("count", 10);
        return map;
    }

    @RequestMapping("generateFile")
    @ResponseBody
    public Map generateFile(MysqlEntity mysqlEntity, String rootPath, String daoPath, String servicePath, String webPath, String mapperPath, String entityPath, String author) throws Exception {

        fastService.generateFile(mysqlEntity, rootPath, daoPath, servicePath, webPath, mapperPath, entityPath, author);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "文件已生成");
        return map;
    }



}
